package com.situ.lession1226.api;

import com.github.pagehelper.PageInfo;
import com.situ.lession1226.model.Review;
import com.situ.lession1226.service.ReviewService;
import com.situ.lession1226.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *
 * 对评论进行展示和删除
 */
@RestController
@RequestMapping(value = "/api/v1/controlreview", produces = "application/json;charset=utf-8")
public class ControlReviewApi {
    private ReviewService reviewService;

    @Autowired
    public void setReviewService(ReviewService reviewService) {
        this.reviewService = reviewService;
    }

    /**
     * 根据传的数据，返回评论集合
     */
    @GetMapping("/allget")
    public Map<String, Object> list(@RequestParam(defaultValue = "1") Integer pageNo,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    Review review) {
        //创建一个分页对象
        PaginateInfo pi = PaginateInfo.of(pageNo, pageSize);

        List<Review> reviews = reviewService.findAllBy(pi, review);

        //PageHelper的分页对象
        PageInfo<?> pif = pi.getPageInfo();

        return Map.of("reviews", reviews, "success", true, "pi", pif);
    }
    @GetMapping("/myget")
    public Map<String, Object> lit(@RequestParam(defaultValue = "1") Integer pageNo,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    Review review) {
        //创建一个分页对象
        PaginateInfo pi = PaginateInfo.of(pageNo, pageSize);

        List<Review> reviews = reviewService.findMy(pi, review);

        //PageHelper的分页对象
        PageInfo<?> pif = pi.getPageInfo();

        return Map.of("reviews", reviews, "success", true, "pi", pif);
    }


    /**
     */
    @DeleteMapping
    public Map<String, Object> deleteByIds(Integer[] ids) {
        int rows = reviewService.deleteByIds(ids);//受影响的行数
        return Map.of("success", true, "rows", rows);
    }

}
